package com.example.teatea.dao;

import com.example.teatea.project.Good;
import com.example.teatea.project.Member;
import com.example.teatea.project.Staff;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class Update {
    public int staffUpdate(Staff staff){
        int result = 0;
        Connection connection = null;;
        Properties properties = new Properties();
        PreparedStatement presta = null;
        try {
            properties.load(new FileInputStream("src\\main\\resources\\com\\example\\teatea\\sql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String Driver = properties.getProperty("Driver");
            String url = properties.getProperty("url");
            Class.forName(Driver);
            String sql = "update staff set sSex='" + staff.getsSex() + "',sName='" + staff.getsName() + "' where sPhone='" + staff.getsPhone() + "'";
            connection = DriverManager.getConnection(url,user,password);
            presta = connection.prepareStatement(sql);
            result = presta.executeUpdate(sql);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }
    public int memberUpdate(Member member){
        int result = 0;
        Connection connection = null;;
        Properties properties = new Properties();
        PreparedStatement presta = null;
        try {
            properties.load(new FileInputStream("src\\main\\resources\\com\\example\\teatea\\sql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String Driver = properties.getProperty("Driver");
            String url = properties.getProperty("url");
            Class.forName(Driver);
            String sql = "update members set mSex='" + member.getmSex() + "',mName='" + member.getmName() + "',mBirthday='" + member.getmBirthday() +"' where mPhone='" + member.getmPhone() + "'";
            connection = DriverManager.getConnection(url,user,password);
            presta = connection.prepareStatement(sql);
            result = presta.executeUpdate(sql);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }
    public int goodUpdate(Good good,String oldname){
        int result = 0;
        Connection connection = null;;
        Properties properties = new Properties();
        PreparedStatement presta = null;
        try {
            properties.load(new FileInputStream("src\\main\\resources\\com\\example\\teatea\\sql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String Driver = properties.getProperty("Driver");
            String url = properties.getProperty("url");
            Class.forName(Driver);
            int n = 0;
            switch (good.getgType()){
                case "茶": n = 1;break;
                case "咖啡": n = 2;break;
                case "便餐": n = 3;break;
                case "娱乐项目": n = 4;break;
            }
            String sql = "update goods set gName='" + good.getgName() + "',gPrice='" + good.getgPrice() + "',gNumber='" + good.getgNumber() +"',gType='" + n + "',gPhoto='" + good.getgImage() + "' where gName='" + oldname + "'";
            connection = DriverManager.getConnection(url,user,password);
            presta = connection.prepareStatement(sql);
            result = presta.executeUpdate(sql);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }
}
